/**
 * 
 */
package edu.ou.cs.youming.jaxb.types.attributes;

import edu.ou.cs.youming.jaxb.types.Attribute;
import edu.ou.cs.youming.jaxb.types.Feature;

/**
 * Attribute that checks for the strength of a word's sentiment value for a feature
 * 
 * @author Youming Lin
 * 
 */
public final class WSAttribute implements Attribute {
	private final String token;
	private final double value;
	private final double size;
	private final int intervals;

	public WSAttribute(final String token, final double value, final double size,
			final int intervals) {
		if (token == null) {
			throw new NullPointerException("token");
		}

		if (size <= 0) {
			throw new IllegalArgumentException("size not positive");
		}

		if (intervals <= 0) {
			throw new IllegalArgumentException("intervals not positive");
		}

		this.token = token;
		this.value = value;
		this.size = size;
		this.intervals = intervals;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.ou.cs.youming.jaxb.types.Attribute#featureIndex(edu.ou.cs.youming.jaxb.types.Feature)
	 */
	@Override
	public final int featureIndex(final Feature f) {
		// return a number in the range of [0, intervals]
		final Integer wf = f.frequencies.get(token);

		if ((wf == null) || (wf <= 0)) {
			return 0;
		}

		final int interval = (int) (Math.abs(value) / size);

		if (interval >= intervals - 1) {
			return intervals - 1;
		}

		return interval + 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.ou.cs.youming.jaxb.types.Attribute#numberOfIntervals()
	 */
	@Override
	public final int numberOfIntervals() {
		return intervals;
	}
}